﻿package com.tudou.track
{
    import com.tudou.model.*;
    import com.tudou.newPlayer.*;
    import com.tudou.utils.*;
    import flash.events.*;
    import flash.net.*;
    import flash.utils.*;

    public class GlobalTracker extends Object
    {
        protected var sendUrl:String;
        protected var _playTimeJsTracker:PlayTimeJsTracker;
        protected var bPaused:Boolean;
        protected var playerStatus:IPlayerStatus;
        protected var splitStr:String = "~_~";
        protected var sendTimer:Timer;
        protected var baseInfo:IMainInfo;
        private static var lock:Boolean = true;
        private static var instance:GlobalTracker = null;

        public function GlobalTracker()
        {
            if (!lock)
            {
                _playTimeJsTracker = PlayTimeJsTracker.ins;
            }
            else
            {
                throw new Error("This is a singleton class GlobalTracker!");
            }
            return;
        }// end function

        public function startSendLog() : void
        {
            if (sendTimer == null)
            {
                sendTimer = new Timer(5000);
                sendTimer.addEventListener(TimerEvent.TIMER, sendTimerHlr);
            }
            sendTimer.reset();
            bPaused = false;
            return;
        }// end function

        public function init(param1:IMainInfo, param2:IPlayerStatus) : void
        {
            this.baseInfo = param1;
            this.playerStatus = param2;
            this.playerStatus.addEventListener(PlayerEvent.CHANGE_VIDEO_STATUS, changeVideoStatusHlr);
            return;
        }// end function

        protected function sendTimerHlr(event:Event) : void
        {
            var _loc_2:Array = [];
            if (!bPaused)
            {
                sendLog("", int(playerStatus.loadTime).toString());
                _playTimeJsTracker.notifyJs(baseInfo.iid, Math.floor(playerStatus.playTime));
            }
            else if (bPaused && sendTimer.currentCount % 6 == 1)
            {
                sendLog(_loc_2.join(splitStr), int(playerStatus.loadTime).toString());
                _playTimeJsTracker.notifyJs(baseInfo.iid, Math.floor(playerStatus.playTime));
            }
            return;
        }// end function

        protected function stopSend() : void
        {
            sendTimer.stop();
            return;
        }// end function

        protected function pauseSend() : void
        {
            bPaused = true;
            return;
        }// end function

        protected function changeVideoStatusHlr(event:PlayerEvent) : void
        {
            switch(playerStatus.videoStatus.status)
            {
                case VideoStatus.PLAYING:
                {
                    continueSend();
                    break;
                }
                case VideoStatus.PAUSED:
                {
                    pauseSend();
                    break;
                }
                case VideoStatus.COMPLETED:
                case VideoStatus.STOPPED:
                {
                    stopSend();
                    break;
                }
                case VideoStatus.READY:
                {
                    if (CDNUtil.isTudouCDN(playerStatus.cdnURL))
                    {
                        sendUrl = URLUtil.getDomain(playerStatus.cdnURL) + "pos";
                    }
                    else
                    {
                        sendUrl = CDNUtil.CDN_TRACK_URL + "pos";
                    }
                    firstSend();
                    continueSend();
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        protected function continueSend() : void
        {
            bPaused = false;
            sendTimer.start();
            return;
        }// end function

        private function firstSend() : void
        {
            var _loc_1:Array = [];
            _loc_1.push(2);
            _loc_1.push(baseInfo.cvid);
            _loc_1.push(baseInfo.videoTotalTime);
            _loc_1.push(baseInfo.sourceId);
            _loc_1.push(baseInfo.channelId);
            _loc_1.push(baseInfo.areaCode);
            _loc_1.push(baseInfo.userId);
            _loc_1.push(baseInfo.lid);
            _loc_1.push(baseInfo.iid);
            _loc_1.push(baseInfo.fuid);
            _loc_1.push(baseInfo.juid);
            _loc_1.push(baseInfo.href);
            _loc_1.push(baseInfo.refferURL);
            _loc_1.push(baseInfo.currRateType);
            sendLog(_loc_1.join(splitStr));
            _playTimeJsTracker.notifyJs(baseInfo.iid, baseInfo.lastCurPlayTime);
            return;
        }// end function

        protected function sendLog(param1:String = "", param2:String = "") : void
        {
            var _loc_3:* = sendUrl;
            _loc_3 = URLUtil.addKeyValue(_loc_3, "act", "dur");
            _loc_3 = URLUtil.addKeyValue(_loc_3, "dk", baseInfo.rid);
            _loc_3 = URLUtil.addKeyValue(_loc_3, "pt", Math.ceil(playerStatus.hasPlayedTime / 1000).toString());
            if (param2)
            {
                _loc_3 = URLUtil.addKeyValue(_loc_3, "de", param2);
            }
            if (param1)
            {
                _loc_3 = URLUtil.addKeyValue(_loc_3, "ds", encodeURIComponent(param1));
            }
            var _loc_4:* = new URLRequest(_loc_3);
            new URLRequest(_loc_3).method = URLRequestMethod.GET;
            sendToURL(_loc_4);
            return;
        }// end function

        public static function getInst() : GlobalTracker
        {
            if (instance == null)
            {
                lock = false;
                instance = new GlobalTracker;
                lock = true;
            }
            return instance;
        }// end function

    }
}
